//
//  UIImageView+Round.m
//  GuiBao
//
//  Created by 文彬 on 2016/11/24.
//  Copyright © 2016年 文彬. All rights reserved.
//

#import "UIImageView+Round.h"

@implementation UIImageView (Round)


/**
 设置一个圆角图片

 @param url 图片地址
 @param holdImage 占位图片
 */
- (void)setRoundImageWithURL:(NSString *)url placeholderImage:(UIImage*)holdImage
{
    NSString *key = [NSString stringWithFormat:@"%@withround",url];
    UIImage *roundImage =[[SDWebImageManager sharedManager].imageCache imageFromMemoryCacheForKey:key];
    if (roundImage!=nil) {
        
        self.image = roundImage;
        return;
    }
    
    if ([[SDWebImageManager sharedManager].imageCache diskImageExistsWithKey:key]) {
        
        self.image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:key];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [self setImageWithURL:[[NSURL alloc]initWithString:url] placeholderImage:holdImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
        
        if (image!=nil) {
            //TODO  获取圆形图片放到异步线程里去
            UIImage *circleImage =  [StaticTools circleImage:image withParam:2];
            weakSelf.image = circleImage;
            [[SDWebImageManager sharedManager].imageCache  storeImage:circleImage forKey:key toDisk:YES];
        }
    }];
}

@end
